package com.lgr.store.controller;


import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lgr.store.service.MaterialTypeService;
import com.lgr.store.utils.AppointStateEnum;
import com.lgr.store.utils.LogJournal;
import com.lgr.store.utils.PageObject;
import com.lgr.store.vo.MaterialTypeVo;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin/materialType")
public class MaterialTypeController {
    @Resource
    MaterialTypeService materialTypeService;
    @Resource
    HttpSession session;

    @RequestMapping(value="/list")
    public String list(HttpServletRequest request, MaterialTypeVo materialTypeVo, PageObject pager, ModelMap map){
        int cnt = 0;
        try{
            cnt = materialTypeService.count(materialTypeVo);
            System.out.println("....................."+cnt);
        }catch(Exception e){
            e.printStackTrace();
        }
        pager.setTotalRows(cnt);
        List<MaterialTypeVo> materialTypeList = materialTypeService.list(materialTypeVo,pager);
        map.addAttribute("materialTypeVo",materialTypeVo);
        pager.setDatas(materialTypeList);
        map.addAttribute("pager",pager);

        return "materialTypeList";
    }
    //新增或修改
    @RequestMapping(value = "/add")
    public String add(HttpServletRequest request, ModelMap map, MaterialTypeVo materialTypeVo, @RequestParam(value = "materialTypeId")Integer materialTypeId){
        if(materialTypeService.findById(materialTypeId) != null){
            //修改
            session.setAttribute(AppointStateEnum.LOG.getStateInfo(),LogJournal.setLogParam(2,materialTypeVo));
            materialTypeService.update(materialTypeVo);
        }else{
            //新增
            session.setAttribute(AppointStateEnum.LOG.getStateInfo(),LogJournal.setLogParam(1,materialTypeVo));
            materialTypeService.add(materialTypeVo);
        }
        PageObject pager = new PageObject();
        pager.setCur_page(1);
        //重新设置查询条件
        materialTypeVo = new MaterialTypeVo();
        return list(request,materialTypeVo,pager,map);
    }
    //删除
    @RequestMapping(value = "/del")
    public String del(HttpServletRequest request,Integer materialTypeId,MaterialTypeVo materialTypeVo,ModelMap map){
        session.setAttribute(AppointStateEnum.LOG.getStateInfo(),LogJournal.setLogParam(3,materialTypeVo));
        materialTypeService.delete(materialTypeId);
        PageObject pager = new PageObject();
        pager.setCur_page(1);
        //重新设置查询条件
        materialTypeVo= new MaterialTypeVo();
        return list(request,materialTypeVo,pager,map);
    }
    @RequestMapping(value = "/json")
    @ResponseBody
    public Map json(MaterialTypeVo materialTypeVo) throws IOException {
        MaterialTypeVo materialTypeList = materialTypeService.findById(materialTypeVo.getMaterialTypeId());
        Map map=new HashMap();
        map.put("materialTypeList",materialTypeList);
        return map;
    }
}
